#!/bin/sh

#
# Test streaming a firmware update through fwup.
# This simulates the case where there's no local storage to hold
# the firmware update while it is being applied.
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
file-resource TEST {
	host-path = "${TESTFILE_1K}"
}
task complete {
	on-resource TEST { raw_write(1) }
}
EOF

# Create the firmware file like normal
$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Pipe the contents of the firmware file through fwup
cat $FWFILE | $FWUP_APPLY -a -d $IMGFILE -i - -t complete

# The firmware file is equivalent to the following dd call
dd if=$TESTFILE_1K seek=1 of=$WORK/check.bin 2>/dev/null
cmp_bytes 1024 $TESTFILE_1K $IMGFILE 0 512

# Try it again, but redirect the input this time
# NOTE: This fails on Windows due to an fstat call in libarchive
#       returning error. Amazingly pipes work on Windows.
#rm $IMGFILE
#$FWUP_APPLY -a -d $IMGFILE -i - -t complete < $FWFILE
#diff $WORK/check.bin $IMGFILE
